package com.ruoyi.web.controller.learn;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.LearningContent;
import com.ruoyi.system.service.ILearningContentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用来发布学习内容Controller
 * 
 * @author admin
 * @date 2024-03-06
 */
@Controller
@RequestMapping("/system/content")
public class LearningContentController extends BaseController
{
    private String prefix = "system/content";

    @Autowired
    private ILearningContentService learningContentService;

    @RequiresPermissions("system:content:view")
    @GetMapping()
    public String content()
    {
        return prefix + "/content";
    }

    /**
     * 查询用来发布学习内容列表
     */
    @RequiresPermissions("system:content:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LearningContent learningContent)
    {
        startPage();
        List<LearningContent> list = learningContentService.selectLearningContentList(learningContent);
        return getDataTable(list);
    }

    /**
     * 导出用来发布学习内容列表
     */
    @RequiresPermissions("system:content:export")
    @Log(title = "用来发布学习内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LearningContent learningContent)
    {
        List<LearningContent> list = learningContentService.selectLearningContentList(learningContent);
        ExcelUtil<LearningContent> util = new ExcelUtil<LearningContent>(LearningContent.class);
        return util.exportExcel(list, "用来发布学习内容数据");
    }

    /**
     * 新增用来发布学习内容
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用来发布学习内容
     */
    @RequiresPermissions("system:content:add")
    @Log(title = "用来发布学习内容", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody LearningContent learningContent)
    {
        learningContent.setCreatorId(String.valueOf(getUserId()));
        return toAjax(learningContentService.insertLearningContent(learningContent));
    }

    /**
     * 修改用来发布学习内容
     */
    @RequiresPermissions("system:content:edit")
    @GetMapping("/edit/{contentId}")
    public String edit(@PathVariable("contentId") Long contentId, ModelMap mmap)
    {
        LearningContent learningContent = learningContentService.selectLearningContentByContentId(contentId);
        mmap.put("learningContent", learningContent);
        return prefix + "/edit";
    }

    /**
     * 修改保存用来发布学习内容
     */
    @RequiresPermissions("system:content:edit")
    @Log(title = "用来发布学习内容", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody LearningContent learningContent)
    {
        return toAjax(learningContentService.updateLearningContent(learningContent));
    }

    /**
     * 删除用来发布学习内容
     */
    @RequiresPermissions("system:content:remove")
    @Log(title = "用来发布学习内容", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(learningContentService.deleteLearningContentByContentIds(ids));
    }
}
